package com.dragon.app.controller.attendance;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.app.config.annotation.AddPage;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.logical.service.avttendance.IAttendanceLeaveService;
import com.dragon.repository.domain.dto.AttendanceLeaveDTO;
import com.dragon.repository.domain.vo.AttendanceLeaveVO;
import com.dragon.repository.entity.AttendanceLeave;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 请假表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-07-20
 */
@RestController
@RequestMapping("attendanceLeave")
@Api(tags = "考勤请假")
public class AttendanceLeaveController {

    @Autowired
    IAttendanceLeaveService service;

    @GetMapping("list")
    @ApiOperation("获取请假列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true, type = "int"),
            @ApiImplicitParam(name = "adminId", value = "用户id",required = false, type = "int")
    })
    public ResponseData<List<AttendanceLeaveDTO>> leaveList( @RequestParam Integer page,
                                                             @RequestParam Integer limit ,
                                                             @RequestParam(required = false) Integer adminId){
        Page<AttendanceLeave> leavePage = service.leaveList(page, limit, adminId);
        return ResponseData.success(AttendanceLeaveDTO.convertList(leavePage.getRecords()),leavePage.getTotal());
    }

    @PostMapping("add")
    @ApiOperation("添加请假")
    @AddPage
    public ResponseData addLeave(@RequestBody @Validated(Insert.class)AttendanceLeaveVO vo){
        return ResponseData.success(service.addLeave(AttendanceLeaveVO.convert(vo)));
    }

    @PostMapping("detail")
    @ApiOperation("详情")
    public ResponseData<AttendanceLeaveDTO> detailLeave(
            @ApiParam(name = "id", value = "id", required = true, type = "int")
            @RequestParam Integer id){
        AttendanceLeave leave = service.detailLeave(id);
        return ResponseData.success(AttendanceLeaveDTO.convert(leave));
    }

}
